package com.aji.silk1.controller;

import com.aji.allcommon.result.Result;
import com.aji.allcommon.service.FileStorageService;
import com.aji.allcommon.utills.MultipartFileUtill;
import com.aji.silk1.config.FileEnvConfig;
import com.aji.silk1.domain.Sep;
import com.aji.silk1.domain.Silk;
import com.aji.silk1.domain.dto.SilkConditionDto;
import com.aji.silk1.domain.dto.SilkDto;
import com.aji.silk1.mapper.SepMapper;
import com.aji.silk1.service.SepService;
import com.aji.silk1.service.SilkService;
import com.aji.silk1.utils.FileUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/silk1")
@Api(tags = "现代丝绸样品模块")
@CrossOrigin
@RequiredArgsConstructor
public class Silk1Controller {

    private final SilkService silkService;
    private final FileStorageService fileStorageService;
    private final SepService sepService;
    private final SepMapper sepMapper;
    private final FileEnvConfig fileEnvConfig;



    @ApiOperation("图片转url地址")
    @PostMapping("/imgToUrl")
    public Result imgToUrl(MultipartFile imgFile) throws IOException {
        String originalFilename = imgFile.getOriginalFilename();
        String simpFileName = originalFilename.substring(0, originalFilename.length() - 4) + ".jpg";
        File fileimg = File.createTempFile("temp", null);
        imgFile.transferTo(fileimg);
        FileInputStream simpJpgFile = MultipartFileUtill.multipartTifToJpgFileInputStream(fileimg);
        String accessSimpFileName = fileStorageService.uploadFile("img/jpg", simpFileName, simpJpgFile, "img/jpg");
        return Result.success(accessSimpFileName);
    }
    @ApiOperation("新增丝绸信息")
    @PostMapping()
    public Result save(SilkDto silkDto) {
        silkService.add(silkDto);
        return Result.success("新增成功！");
    }
    @ApiOperation("删除丝绸信息")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        QueryWrapper<Sep> sepWrapper = new QueryWrapper<>();
        sepWrapper.in("silk_id", ids);
        sepMapper.delete(sepWrapper);
        silkService.removeByIds(ids);
        return Result.success("删除成功！");
    }
    
    @ApiOperation("查询丝绸信息 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        return Result.success(silkService.getById(id));
    }

    @ApiOperation("根据丝绸id查询光谱")
    @GetMapping("/silk/{id}")
    public Result getSepByLeatherId(@PathVariable Long id){
        LambdaQueryWrapper<Sep> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Sep>().select()
                .eq(id!=null,Sep::getSilkId,id);
        List<Sep> sepList = sepService.list(compoundLambdaQueryWrapper);
        return Result.success(sepList);
    }

    @ApiOperation("查询全部丝绸信息")
    @GetMapping("/list")
    public Result list(){
        return Result.success(silkService.list(null));
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, SilkConditionDto compound){
        //合法检验
        if(page<1||pageSize<1){return Result.error("分页参数异常");}
        Page<Silk> compoundPage= new Page<>(page,pageSize);
        LambdaQueryWrapper<Silk> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Silk>().select()
                .like(compound.getSilkNumber()!=null,Silk::getSilkNumber,compound.getSilkNumber())
                .like(compound.getSilkName()!=null,Silk::getSilkName,compound.getSilkName())
                .ge(compound.getTemp1()!=null,Silk::getTemperature,compound.getTemp1())
                .lt(compound.getTemp2()!=null,Silk::getTemperature,compound.getTemp2())
                .ge(compound.getHumidity1()!=null,Silk::getHumidity,compound.getHumidity1())
                .lt(compound.getHumidity2()!=null,Silk::getHumidity,compound.getHumidity2());
        IPage<Silk> compoundIPage = silkService.page(compoundPage, compoundLambdaQueryWrapper);
        return Result.success(compoundIPage);

    }

    @ApiOperation("获取 demo文件")
    @GetMapping("/demoFile")
    public ResponseEntity<byte[]> getDemoFile() throws IOException {
        // 拼接文件路径
        String fullPath = fileEnvConfig.getDataPath() + fileEnvConfig.getDemoFile();
        String fileName = fileEnvConfig.getDemoFile();

        // 使用工具类读取文件为 byte[]
        return FileUtils.transformFile(fullPath, fileName);
    }

    @ApiOperation("获取数据总量")
    @GetMapping("/count")
    public Result count(){
        int count = Math.toIntExact(silkService.count());
        return Result.success(count);
    }
}
